import {extend, ResponseError} from "umi-request";
import {history} from "@umijs/max";
import {message} from "antd";
const loginPath = '/user/login';


const errorHandler = (error: ResponseError): undefined => {

    message.error(error?.message);
  console.log("处理1");
  };

const request = extend({
  prefix:process.env.NODE_ENV==='production'?'http://47.121.130.221':undefined   ,
  errorHandler,

})

request.interceptors.response.use(async (response): Promise<any> => {



  const data = await response.clone().json();
  // alert(data);
  // console.log(data);

  if (data?.code === 40010) {
    history.push(loginPath);
    const  error: any = new Error(data.description);
    throw error
  }
  if (data?.code !== 0) {
    const  error: any = new Error(data.description);
    throw error
  }

  return data.data;

});


export default request;
